﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;

namespace Eleanor.DAL
{
    public class UserDAL:DALBase
    {
        public int Insert(dynamic user)
        {
            var command = DefaultContext.StoredProcedure("UserInsert")
                .Parameter("UserName", user.UserName as string)
                .Parameter("Password", user.Password as string)
                .ParameterOut("UserId", DataTypes.Int32, 4);
            command.Execute();
            return command.ParameterValue<int>("UserId");
        }

        public dynamic Select(string userName)
        {
            var command = DefaultContext
                       .Sql("UserSelectByName")
                       .CommandType(DbCommandTypes.StoredProcedure)
                       .Parameter("UserName", userName);
            return command.QuerySingle<dynamic>();
        }

        public List<dynamic> SelectAll()
        {
            var command = DefaultContext
                        .Sql("UserSelectALL")
                        .CommandType(DbCommandTypes.StoredProcedure);
            return command.QueryMany<dynamic>();
        }

        public bool Delete(int userId)
        {
            var command = DefaultContext
                        .StoredProcedure("UserDelete")
                        .Parameter("UserId", userId);
            return command.Execute() > 0;
        }

        public int UpdatePassword(dynamic user)
        {
            return DefaultContext.Update("[user]")
                            .Column("password", user.Password as string)
                            .Column("update_date", DateTime.Now)
                            .Where("user_id", (int)user.UserId)
                            .Execute();
        }
    }
}
